/*
 * @Author: 黄达全
 * @Date: 2024-07-19 23:21:19
 * @LastEditTime: 2024-07-29 16:31:09
 */
import { createRouter, createWebHashHistory } from 'vue-router';
import type { RouterOptions, Router, RouterHistory } from 'vue-router';
import systemRouter,{asyncRoutes} from "./systemRouter"
import type {MenuItem} from "@/common/type"
// 系统路由
const routes = systemRouter

const router = createRouter({
	history: createWebHashHistory() as RouterHistory,
	routes: routes
} as RouterOptions) as Router;






export const addRoute = (menus_arr:MenuItem[]) => {
	//判断是否已加载过动态/静态路由
	let isGetRouter: boolean = false;
	
	const findAndAddRoute = (menus: MenuItem[]) => {
		menus.forEach(item1=>{
			// 过滤出需要添加的路由
			const route=asyncRoutes.find(item2=>item2.path==item1.frontpath)
			// 判断路由是否存在且路由此前不存在
			if(route&&!router.hasRoute(route.name!)){
				router.addRoute("admin",route)
				isGetRouter = true;
			}
			if(item1.child&&item1.child.length>0){
				findAndAddRoute(item1.child)
			}
		})
	}
	findAndAddRoute(menus_arr)
	return isGetRouter
}


export default router